package studio.raptor.ddal.dashboard.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import studio.raptor.ddal.dashboard.common.Constants;
import studio.raptor.ddal.dashboard.log.bo.ConnPoolLogExtendData;
import studio.raptor.ddal.dashboard.log.bo.SqlProcessLogExtendData;
import studio.raptor.ddal.dashboard.service.interfaces.DdalMonitorService;
import studio.raptor.ddal.dashboard.service.interfaces.SystemConfigService;

import java.util.List;


@RestController
public class DdalMonitorController {

    private static Logger log = LoggerFactory.getLogger( DdalMonitorController.class);

    @Autowired
    private DdalMonitorService ddalMonitorService;

    @Autowired
    private SystemConfigService systemConfigService;

    @RequestMapping(value = "/ddalmonitor/grafanaAddress", method = RequestMethod.GET)
    public String getGrafanaAddress(){
        return systemConfigService.getGrafanaAddress();
    }

    @RequestMapping(value = "/ddalmonitor/{acceptBeginTime}/{acceptEndTime}/{appName}/list", method = RequestMethod.GET)
    public List<SqlProcessLogExtendData> getSqlProcessLogData(@PathVariable String acceptBeginTime,
                                                          @PathVariable String acceptEndTime,
                                                          @PathVariable String appName,
                                                          @RequestParam(value = "sqlType", required = false) String sqlType,
                                                          @RequestParam(value = "processTime", required = false) String processTime,
                                                          @RequestParam(value = "success", required = false) String success,
                                                          @RequestParam(value = "page", required = false) Integer page) {
        return ddalMonitorService.getSqlProcessLogData(acceptBeginTime,acceptEndTime,appName,sqlType,processTime,success,page.intValue(), Constants.DEFAULT_PAGE_VOLUME);
    }

    @RequestMapping(value = "/connmonitor/{acceptBeginTime}/{acceptEndTime}/{appName}/list", method = RequestMethod.POST)
    public List<ConnPoolLogExtendData> getConnPoolLogData(@PathVariable String acceptBeginTime,
                                                          @PathVariable String acceptEndTime,
                                                          @PathVariable String appName,
                                                          @RequestParam(value = "username", required = false) String username,
                                                          @RequestParam(value = "page", required = false) Integer page) {
        return ddalMonitorService.getConnPoolLogData(acceptBeginTime,acceptEndTime,appName,username,page.intValue(), Constants.DEFAULT_PAGE_VOLUME);
    }
}
